<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/4
 * Time: 上午11:24
 */

namespace app\api\controller;

use Alipay\Alipayobj;
use app\common\controller\ActionLogs;
use app\common\controller\Bonus;
use app\common\controller\CardDedu;
use app\common\controller\FeiePrint;
use app\common\controller\TemplateMsg;
use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\WxOpen;
use think\Db;
use Wechat\Wxobjopen;

class Pay extends Pub
{
    public function getpay($param)
    {

        try {
            $pricelen = $this->getFloatLength($param['total_fee']);
            if ($pricelen <= 2) {
                if ($param['total_fee'] > 0 && $param['total_fee'] <= 100000) {
                    if ($param['total_fee'] >= 0.01) {
                        $Paylog = new Paylog();
                        if (isset($param['client_source']) && $param['client_source'] !== 1) {
                            $param['orderid'] = $this->getorderid($param['merchantid'], "wsy");


                        } else {
                            $param['orderid'] = $this->getorderid($param['merchantid']);
                        }

                        $pays['source'] = $param['source'];
                        //款台
                        if (isset($param['kuantai'])) {
                            $pays['kuantai'] = $param['kuantai'];
                        } else {
                            if ($pays['source'] == "PC" || $pays['source'] == "pc") {
                                $pays['kuantai'] = $param['device_id'];

                            } else {
                                $pays['kuantai'] = $pays['source'];
                            }
                        }
                        $orderchack = $Paylog->where(array('orderid' => $param['orderid']))->find();
                        if (!$orderchack) {
                            $pays['mid'] = $param['merchantid'];
                            $pays['add_time'] = time();
                            $pays['price'] = $param['total_fee'];
                            $pays['yf_price'] = $param['total_fee'];
                            $pays['pay_statu'] = 0;
                            $pays['d_cashier'] = $param['device_id'];
                            $pays['pay_cate'] = empty($param['pay_cate']) ? "pay" : $param['pay_cate'];
                            if (isset($param['client_source']) && $param['client_source'] !== 1) {
                                $pays['trade_no'] = $param['trade_no'];
                                $pays['client_source'] = $param['client_source'];

                            }
                            //2019-10-26 添加
                            $pays['remark'] = !empty($param['remark']) ? $param['remark']:"";     //备注
                            //END
                            $kt = substr($param['auth_code'], 0, 2);
                            $User = new Merchant();
                            $machinfos = $User->where(array('id' => $param['merchantid']))->find();

                            if ($kt >= 25 && $kt <= 30) {
                                $pays['pay_way'] = $machinfos['ali_way'];
                                switch ($machinfos['ali_way']) {

                                    case '0':
                                        $Officialpay = new OfficialPay();
                                        return $Officialpay->getpay($param, $pays);
                                        break;
                                    case '1':
                                        $Officialpay = new OfficialPay();
                                        return $Officialpay->mspay($param);
                                        break;
                                    case '2':

                                        $NotCardPay = new NotCardPay();
                                        $rarr = $NotCardPay->getpay($param, $pays);
                                        break;
                                    case '10':

                                        $Lftpay = new Lftpay();
                                        $rarr = $Lftpay->pay($param, $pays);
                                        break;
                                    case '30':
                                        $Huifupay = new Huifupay();
                                        $rarr = $Huifupay->pay($param, $pays);
                                        break;
                                }
                            } elseif ($kt >= 10 && $kt <= 15) {
                                $pays['pay_way'] = $machinfos['wechat_way'];
                                switch ($machinfos['wechat_way']) {
                                    case '0':
                                        $Officialpay = new OfficialPay();
                                        return $Officialpay->getpay($param, $pays);
                                        break;
                                    case '1':
                                        $Officialpay = new OfficialPay();
                                        $Paylog->save($pays);
                                        return $Officialpay->mspay($param);
                                        break;
                                    case '2':

                                        $NotCardPay = new NotCardPay();
                                        $rarr = $NotCardPay->getpay($param, $pays);
                                        break;
                                    case '10':

                                        $Lftpay = new Lftpay();
                                        $rarr = $Lftpay->pay($param, $pays);
                                        break;
                                    case '30':
                                        $Huifupay = new Huifupay();
                                        $rarr = $Huifupay->pay($param, $pays);
                                        break;
                                }

                            } else {
                                $rarr['return_code'] = "SUCCESS";
                                $rarr['return_msg'] = '';
                                $rarr['result_code'] = 'FAIL';
                                $rarr['result_msg'] = '条码格式错误';
                            }
                        } else {
                            $rarr['return_code'] = "SUCCESS";
                            $rarr['return_msg'] = '';
                            $rarr['result_code'] = 'FAIL';
                            $rarr['result_msg'] = '订单编号已存在，请重新发起支付';
                        }
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['return_msg'] = '';
                        $rarr['result_code'] = 'FAIL';
                        $rarr['result_msg'] = '支付金额必须大于0.01';
                    }
                } else {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = '';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = '请输入正确的金额';
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '支付金额必须大于0元小于10万元';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }


    public function getcheckpay($param)
    {
        try {
            $map['orderid'] = $param['out_trade_no'];
            $map['mid'] = $param['merchantid'];
            $Paylog = new Paylog();
            $payinfo = $Paylog->where($map)->field('pay_way,pay_fs,pay_cate')->find();
            $pay_way = $payinfo['pay_way'];
            $pay_fs = $payinfo['pay_fs'];
            $pay_cate = $payinfo['pay_cate'];
            if ($pay_cate == "wx_deposit") {
                $DepositPay = new DepositPay();
                $rarr = $DepositPay->wxcheckpay($param);
            } else {
                switch ($pay_way) {
                    case '0':
                        $Officialpay = new OfficialPay();
                        $rarr = $Officialpay->getcheckpay($param, $pay_fs);
                        break;
                    case '1':
                        $rarr = $this->mscheckpay($param);
                        break;
                    case '2':
                        $NotCardPay = new NotCardPay();
                        $rarr = $NotCardPay->getcheckpay($param, $pay_fs);
                        break;
                    case "10":
                        $Lftpay = new Lftpay();
                        $rarr = $Lftpay->checkpay($param);
                        break;
                    case "30":
                        $Lftpay = new Huifupay();
                        $rarr = $Lftpay->checkpay($param);
                        break;
                }
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;

    }


}